module.exports   B
last analyzed

Complexity

Conditions 1
Paths 32

Size

Total Lines 87
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 56
c 0
b 0
f 0
nc 32
dl 0
loc 87
rs 8.44
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B 0 83 7

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
const Redis = require('./libraries/redis')
2
const Constant = require('./libraries/constant')
3
const ApiError = require('./util/api_error')
4
const _ = require('underscore')
5
const ServiceAudit = require('./services/catering/audit')
6
const ServiceStore = require('./services/catering/store')
7
8
module.exports = function (permission) {
9
10
	return async function (ctx, next) {
11
12
		async function checkToken() {
13
			let token = (typeof (ctx.request.headers.token) == 'undefined' || !ctx.request.headers.token) ?
14
				ctx.cookies.get('token') : ctx.request.headers.token
15
			let uid = (typeof (ctx.request.headers.uid) == 'undefined' || !ctx.request.headers.uid) ?
16
				ctx.cookies.get('uid') : ctx.request.headers.uid
17
18
			if (!token || !uid) {
19
				console.log('token: ' + token)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
20
				console.log('uid: ' + uid)
21
				throw new ApiError('auth.error', 'token missing')
22
			}
23
24
			sessionKey = Constant.CATERING_SESSION + token
0 ignored issues
show
Bug introduced by
The variable sessionKey seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.sessionKey.
Loading history...
25
			session = await Redis.get(sessionKey)
0 ignored issues
show
Bug introduced by
The variable session seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.session.
Loading history...
26
			session = JSON.parse(session)
27
			if (!session) {
28
				throw new ApiError('auth.error', 'token error')
29
			}
30
31
			if (session.uid == uid) {
32
				ctx.uid = uid
33
				return true
34
			} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
35
				throw new ApiError('auth.error', 'no permission')
36
			}
37
			
38
		}
39
40
		async function checkUser() {
41
			await checkToken()
42
			await next()
43
		}
44
45
		async function checkAudit() {
46
			await checkToken()
47
			await isAudit()
48
			await next()
49
		}
50
51
		async function checkStore() {
52
			await checkToken()
53
			await ownerStore()
54
			await next()
55
		}
56
57
		async function isAudit() {
58
			let check = await ServiceAudit.getAudit(ctx.uid)
59
			if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission audit')
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
60
			return true
61
		}
62
63
		async function ownerStore() {
64
			let storeId = ctx.params.storeId
65
			let check = await ServiceStore.getStore(storeId)
66
			if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission store')
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
67
			if (check.seller_id != ctx.uid) throw new ApiError('auth.notPermission')
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
68
			return true
69
		}
70
71
		// 检查header
72
		if (!_.has(ctx.request.headers, 'store-id')) {
73
			throw new ApiError('validate.error', 'store-id')
74
		}
75
		if (!_.has(ctx.request.headers, 'mina-source')) {
76
			throw new ApiError('validate.error', 'mina-source')
77
		}
78
79
		// guest
80
		if (permission === 'guest') {
81
			await next()
82
		} else if (permission === 'user') {
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
83
			return await checkUser()
84
		} else if (permission === 'audit') {
85
			return await checkAudit()
86
		} else if (permission === 'store') {
87
			return await checkStore()
88
		} else {
89
			throw new ApiError('role.notExist')
90
		}
91
92
	}
93
94
}
95